package com.cheer.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.OaOpinionDao;
import com.cheer.dto.OaOpinionQueryDTO;
import com.cheer.entity.OaLearnClassEntity;
import com.cheer.entity.OaOpinionEntity;
import com.cheer.entity.OaUserEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.OaLearnClassService;
import com.cheer.service.OaOpinionService;
import com.cheer.service.OaUserService;
import com.cheer.vo.OaOpinionListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("oaOpinionService")
public class OaOpinionServiceImpl extends ServiceImpl<OaOpinionDao, OaOpinionEntity> implements OaOpinionService {

    @Autowired
    private OaOpinionDao oaOpinionDao;

    @Autowired
    private OaLearnClassService oaLearnClassService;

    @Autowired
    private OaUserService oaUserService;

    @Override
    public Page<OaOpinionListVO> list(OaOpinionQueryDTO opinionQueryDTO) {
        String path = null;
        String name = StringUtils.trimToNull(opinionQueryDTO.getName());
        String nickname = StringUtils.trimToNull(opinionQueryDTO.getNickname());

        if (opinionQueryDTO.getPid() != null && opinionQueryDTO.getPid() != 0) {
            OaLearnClassEntity learnClass = oaLearnClassService.getById(opinionQueryDTO.getPid());
            if (learnClass == null) {
                throw new ServiceException("单位不存在");
            }

            path = learnClass.getPath() + opinionQueryDTO.getPid() + ",";
        }

        PageHelper.startPage(opinionQueryDTO.getPageNum(), opinionQueryDTO.getPageSize());

        List<OaOpinionListVO> list = oaOpinionDao.selectPageByCondition(path, name, nickname);

        Set<String> idSet = new HashSet<>();
        list.forEach(user -> {
            String[] split = StringUtils.split(user.getPath(), ",");
            idSet.addAll(Lists.newArrayList(split));
        });
        idSet.remove("0");

        Map<Integer, String> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(idSet)) {
            oaLearnClassService.listByIds(idSet)
                    .forEach(learnClass -> map.put(learnClass.getId(), learnClass.getName()));
        }

        for (OaOpinionListVO opinionListVO : list) {
            opinionListVO.setAddTime(DateUtil.format(DateUtil.date(Long.valueOf(opinionListVO.getAddTime()) * 1000),
                    DatePattern.NORM_DATETIME_PATTERN));

            String[] split = StringUtils.split(opinionListVO.getPath(), ",");
            if (ArrayUtils.isNotEmpty(split)) {
                List<String> learnClass = new ArrayList<>();
                for (String str : split) {
                    if (map.get(Integer.valueOf(str)) != null) {
                        if (!StringUtils.equals("0", str)) {
                            learnClass.add(map.get(Integer.valueOf(str)));
                        }
                    } else {
                        break;
                    }
                }
                if (CollectionUtils.isNotEmpty(learnClass)) {
                    opinionListVO.setLearnClass(StringUtils.join(learnClass, "-"));
                }
            }

            opinionListVO.setShortLearnClass(map.get(Integer.valueOf(opinionListVO.getDepartment())));
        }

        PageInfo<OaOpinionListVO> pageInfo = new PageInfo<>(list);

        return new Page<OaOpinionListVO>()
                .setPageNum(pageInfo.getPageNum())
                .setPageSize(pageInfo.getPageSize())
                .setPages(pageInfo.getPages())
                .setTotal(pageInfo.getTotal())
                .setContent(list);
    }

    @Override
    public void add(Integer userId, String suggest) {
        OaUserEntity user = oaUserService.getById(userId);
        if (user != null) {
            OaOpinionEntity opinion = new OaOpinionEntity()
                    .setOpenid(user.getOpenid())
                    .setSuggest(suggest)
                    .setAddTime(String.valueOf(DateUtil.currentSeconds()));
            oaOpinionDao.insert(opinion);
        }
    }
}